<?php

session_start();
require_once("../include/MySugarApplication.php");
require_once("../include/CampaignSoap.php");
$app->checkSession('Campaigns/Data.php');

$query='';
$quote = '"';
$deletedQuery = '';
$deleteCount = 0;
$startIndex = 0;
$numberOfRows = 0;
$includeDeleted = false;
$orderby = ' campaigns.date_modified desc';
$recordCountOnly=$_REQUEST["recordCount"];
$keysOnly=$_REQUEST["keysOnly"];
$account_id = $_REQUEST["account_id"];

if ($account_id != "") {

	$query = " accounts.id=".$quote.$account_id.$quote;

} else {
	
	$startIndexStr = $_REQUEST["startIndex"];
	if ($startIndexStr == "")
		$startIndex = 0;
	else
		$startIndex = (int)$startIndexStr;

	$numberOfRowsStr = $_REQUEST["numberOfRows"];
	if ($numberOfRowsStr == "")
		$numberOfRows = 0;
	else
		$numberOfRows = (int)$numberOfRowsStr;

	if ( $recordCountOnly != "Y" ) {

		if ($_REQUEST["lastModifiedOn"] != "" ) {
			$query = " contacts.date_modified >= ";
			$query = $query.$quote.$_REQUEST["lastModifiedOn"].$quote;
	
			$includeDeleted = true;
		}
	}
}

$rows= $contactSoap->getCampaigns($query, $startIndex, $numberOfRows, $orderby, $includeDeleted);
$recordCount = $contactSoap->getTotalRows();


?>
<!DOCTYPE HTML SYSTEM "http://www.bitzermobile.com/bitzer/dtd/HTML401-loose-bitzer.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="bitzer-component" content="data">
		<?php
		if (!$isBitzer) {
        	echo '
			<title>MySugar Campaigns</title>
			<LINK REL=StyleSheet HREF="',$app->appRoot,'include/style.css" TYPE="text/css" MEDIA=screen>
        	<script type="text/javascript" src="',$app->appRoot,'include/niftycube.js"></script>
        	<script type="text/javascript">
            window.onload=function(){
				var width = "1600px";
				setAppRoot("',$app->appRoot,'");
                Nifty("div#container");
                Nifty("div#content,div#nav","same-height small");
                Nifty("div#header,div#footer","small");
                Nifty("ul.postnav a","transparent");
				setWidth("div#container", width);
				setWidth("div#content", width);
				setWidth("div#footer", width);
            }
        </script>';
        } else {
			echo '<title></title>';
		}
		?>    
    </head>
    <body>
    <?php
	if (!$isBitzer) {
		echo '
        <div id="container">
            <div id="header">
            	<table width="100%">
					<tr>
						<td width="95%"><h1>Campaigns</h1></td>
						<td align="right" width="5%"><a href="',$app->appRoot,'Logout.php">Logout</a></td>
					</tr>
				</table>
            </div>
            <div id="content">
            <br>';
	}
	?>
                <table data-bitzer-data="true" data-bitzer-title="Campaigns"
                       data-bitzer-desc="List of contacts"
                       data-bitzer-component="Campaigns"
					   data-bitzer-record-count="<?php echo $recordCount; ?>"
                       <?php if (!$isBitzzer) echo 'border="0" cellspacing="0" cellpadding="0"'; ?>
                       <?php if ($numberOfRows > 0) {
					   		echo 'data-bitzer-start-index="',$startIndex,'" data-bitzer-number-of-rows="',$numberOfRows,'" ';
							}
						?>
                       class="list"  width="100%">
					   
<?php
if( $recordCount > 0 && $recordCountOnly != "Y"){  
?>
						<tr>
						<?php
						if ($keyOnly != "Y" && !$isBitzer) {
							if ($array['deleted'] == "1") {
								echo '<th></th>';
							} else {
                        		echo '<th>Delete</th>';
							}
                        }
						?>
                        <th data-bitzer-field="id" data-bitzer-key-order="1" data-bitzer-ignore-anchor="true">id</th>
                        <?php
						if ($keysOnly != "Y" ) {
						?>
                        	<th data-bitzer-field="photo">Photo</th>
                        	<th data-bitzer-field="salutation">Salutation</th>
                        	<th data-bitzer-field="first_name">First Name</th>
                            <th data-bitzer-field="last_name">Last Name</th>
                            <th data-bitzer-field="account_id" data-bitzer-display-lookup="true">Account ID</th>
                            <th data-bitzer-field="lead_source">Lead Source</th>
                            <th data-bitzer-field="campaign_id" data-bitzer-display-lookup="true">Campaign ID</th>
                            <th data-bitzer-field="title">Title</th>
                            <th data-bitzer-field="department">Department</th>
                            <th data-bitzer-field="assigned_user_id" data-bitzer-display-lookup="true">Assigned User ID</th>
                            <th data-bitzer-field="email1">Email</th>
                            <th data-bitzer-field="phone_work">Work Phone</th>
                            <th data-bitzer-field="phone_mobile">Mobile Phone</th>
                            <th data-bitzer-field="phone_home">Home Phone</th>
                            <th data-bitzer-field="phone_other">Other Phone</th>
                            <th data-bitzer-field="phone_fax">Fax</th>
                            <th data-bitzer-field="do_not_call">Do Not Call</th>
                            <th data-bitzer-field="primary_address_street">Primary Address</th>
                            <th data-bitzer-field="primary_address_city">City</th>
                            <th data-bitzer-field="primary_address_state">State</th>
                            <th data-bitzer-field="primary_address_postalcode">Postal Code</th>
                            <th data-bitzer-field="primary_address_country">Country</th>
                            <th data-bitzer-field="description">Description</th>
                            <th data-bitzer-field="last_modified_on">Last Modified On</th>
                        <?php
						}
						?>
                        </tr>
                        <?php
						foreach($rows as $record){
							$array= $app->nameValuePairToSimpleArray($record['name_value_list']);
                        	echo '<tr';
							if ($array['deleted'] == "1") {
								if (!$isBitzer) {
									$fontStartTag = '<font color="#FF0000"><del>';
									$fontEndTag = '</del></font>';
								}
								echo ' data-bitzer-deleted="true"';
							}
							echo '>';
									
							if ($keyOnly != "Y" && !$isBitzer) {
								if ($array['deleted'] == "1") {
									echo '<td></td>';
								} else {
									echo '<td><a href="SaveCampaign.php?actionType=DELETE&id=', $array['id'],'">Delete</a></td>';
								}
							}
								
							echo '<td>',$fontStartTag;	
							if ($keyOnly != "Y" && !$isBitzer) {
								echo '<a href="CampaignForm.php?actionType=UPDATE&id=', $array['id'],'">',$array['id'],'</a>';
							} else {
								echo $array['id'];
							}
							echo $fontEndTag,'</td>';
								
							if ($keysOnly != "Y") {
									echo '<td>',$fontStartTag,'<IMG SRC="',$array['photo_c'],'">',$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['salutation'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['first_name'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['last_name'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['account_id'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['lead_source'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['campaign_id'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['title'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['department'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['assigned_user_id'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['email1'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['phone_work'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['phone_mobile'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['phone_home'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['phone_other'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['phone_fax'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['do_not_call'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['primary_address_street'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['primary_address_city'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['primary_address_state'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['primary_address_postalcode'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['primary_address_country'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['description'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['date_modified'],$fontEndTag,'</td>';
							}
                            echo '</tr>';
						}
						
						
						
						
						
}
						?>
                </table>
    <?php
	if (!$isBitzer) {
		echo '
                <br>
                <ul class="postnavList">
                    <li><a href="CampaignForm.php">Add New Campaign</a></li>
                </ul>
            </div>
            <div id="footer"></div>
        </div>';
	}
	?>
    </body>
</html>
